<template>
  <div class="container">
    <!-- 新增按钮 -->
    <el-button
      v-if="dialogStatus"
      class="add-btn"
      type="primary"
      icon="el-icon-edit"
      @click="addCompanyDialog"
    >新增用户</el-button>
    <!-- 编辑按钮 -->
    <el-button v-else class="edit-btn" type="primary" size="mini" @click="editCompanyShow">编辑</el-button>

    <!-- 弹窗 -->
    <el-dialog :title="title" :visible.sync="addDialogVisible" width="50%" @close="addFormClosed">
      <!-- 表单 -->
      <el-form
        :model="addCompanys"
        :rules="addCompanysRules"
        ref="addCompanysRef"
        label-position="left"
        label-width="130px"
        class="addCompanyForm"
      >
        <el-form-item label="企业名称" prop="shortName">
          <el-input v-model="addCompanys.shortName" placeholder="请输入企业名称"></el-input>
          <el-checkbox label="是否为名企" v-model="addCompanys.isFamous"></el-checkbox>
        </el-form-item>
        <el-form-item label="所属公司" prop="company">
          <el-input v-model="addCompanys.company" placeholder="请输入企业所属公司"></el-input>
          <span>http://www.tianyancha.com(在此可查询所属公司全程与简称)</span>
        </el-form-item>
        <el-row>
          <el-col :span="12">
            <el-form-item label="城市" prop="province">
              <el-select
                v-model="addCompanys.province"
                placeholder="请选择"
                @change="addCompanys.city=''"
              >
                <el-option
                  v-for="(item, i) in cityData"
                  :key="i"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="7">
            <el-form-item prop="city" label-width="0">
              <el-select v-model="addCompanys.city" placeholder="请选择">
                <el-option
                  v-for="(item1, i1) in cityData[getId].children"
                  :key="i1"
                  :label="item1.label"
                  :value="item1.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="5"></el-col>
        </el-row>
        <el-form-item label="方向(企业标签)" prop="tags">
          <el-input v-model="addCompanys.tags" placeholder="请输入企业标签"></el-input>
        </el-form-item>
        <el-form-item label="备注" prop="remarks">
          <el-input type="textarea" v-model="addCompanys.remarks" placeholder="请输入企业备注"></el-input>
        </el-form-item>
      </el-form>
      <!-- 按钮区域 -->
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogVisible=false">取 消</el-button>
        <el-button type="primary" @click="companyConfirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { add, detail, update } from '@/api/hmmm/companys'
import cityData from '@/api/hmmm/citydata'
export default {
  name: 'CompanysAdd',
  props: {
    dialogStatus: {
      type: Boolean,
      required: true,
    },
    row: {
      type: Object,
    },
  },
  data() {
    return {
      cityData,
      addDialogVisible: false, // 弹窗显示
      // 添加表单数据
      addCompanys: {
        isFamous: false, // 是否名企业
        shortName: '', // 公司简介
        company: '', // 所属公司
        province: '', // 省份
        city: '', // 城市,
        remarks: '', // 备注
        tags: '', // 标签
      },
      // 添加企业的规则
      addCompanysRules: {
        shortName: [{ required: true, message: '请输入企业名称', trigger: 'blur' }],
        tags: [{ required: true, message: '请输入企业标签', trigger: 'blur' }],
        province: [{ required: true, message: '请输入省份', trigger: 'blur' }],
        city: [{ required: true, message: '请输入城市', trigger: 'blur' }],
        remarks: [{ required: false }],
        company: [{ required: false }],
      },
      title: '', // 弹出框的提示文字
    }
  },

  methods: {
    // *显示新增弹窗
    addCompanyDialog() {
      this.title = '新增公司'
      this.addDialogVisible = true
    },
    // *显示编辑弹窗
    async editCompanyShow() {
      this.title = '编辑公司'
      this.addDialogVisible = true
      const { data } = await detail(this.row.id)
      console.log(data)
      if (data.isFamous) {
        data.isFamous = true
      } else {
        data.isFamous = false
      }
      this.addCompanys = data
    },
    // *关闭弹窗,清空数据
    addFormClosed() {
      this.$refs.addCompanysRef.resetFields()
      this.addCompanys.isFamous = false
      // this.addCompanys = {}
    },
    // *提交数据
    companyConfirm() {
      this.$refs.addCompanysRef.validate(async (valid) => {
        if (!valid) return // 验证不通过
        // 打开添加弹窗
        if (this.dialogStatus) {
          const data = await add(this.addCompanys)
          console.log(this.addCompanys)
          this.$message.success('添加成功')
          this.$emit('get-companys')
          this.addFormClosed()
          // 关闭弹窗
          this.addDialogVisible = false
        } else {
          const { id } = this.addCompanys
          const addCompanys = this.addCompanys
          const data = await update({
            id: id,
            addCompanys,
          })
          this.$message.success('修改成功')
          this.addDialogVisible = false
          this.$emit('get-companys')
          this.addFormClosed()
        }
      })
    },
  },
  computed: {
    getId() {
      let ii = this.cityData.findIndex((item, i) => {
        return item.value === this.addCompanys.province
      })
      if (ii === -1) {
        ii = 0
      }
      return ii
    },
  },
}
</script>

<style scoped lang='less'>
.container {
  .addCompanyForm {
    padding: 0 30px;
  }
  .add-btn {
    float: right;
    margin-bottom: 8px;
  }
  .edit-btn {
    float: left;
    margin-right: 10px;
  }
  .dialog-footer {
    display: flex;
    justify-content: center;
    align-items: center;
  }
}
</style>
